//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Reflection;
using OpenLS.Core.Notification;
using OpenLS.Core.Serialization;
using OpenLS.Drawing;

namespace OpenLS.Chart
{
    /// <summary>
    /// Represents the legend of a chart
    /// </summary>
    public class Legend : ViewModelBase, IOfficeXmlSerializable, IShape
    {
        private ExtensionElement _extension;
        private readonly ChartArea _chart;
        private readonly ObservableCollection<LegendEntry> _entries = new ObservableCollection<LegendEntry>();
        private Layout _layout;
        private LegendPosition _legendPos = LegendPosition.Right;
        private bool _overlay;
        private ShapeProperties _spPr;
        private TextBody _txPr;

	public Legend()
	{
	}	
        public Legend(ChartArea chart) //\\ type
        {
            _chart = chart;
        }

        public ExtensionElement Extension
        {
            get { return _extension; }
            set { _extension = value; notifyPropertyChanged("Extension");}
        }


        public bool Overlay
        {
            get { return _overlay; }
            set { _overlay = value; notifyPropertyChanged("Overlay");}
        }

        public ObservableCollection<LegendEntry> Entries
        {
            get { return _entries; }
        }

        public LegendPosition Position
        {
            get { return _legendPos; }
            set { _legendPos = value; notifyPropertyChanged("Position");}
        }

        public Layout Layout
        {
            get { return _layout; }
            set { _layout = value; notifyPropertyChanged("Layout");}
        }

        public ShapeProperties ShapeProperties
        {
            get { return _spPr; }
            set { _spPr = value;notifyPropertyChanged ("ShapeProperties"); }
        }

        public TextBody TextProperties
        {
            get { return _txPr; }
            set { _txPr = value; notifyPropertyChanged("TextProperties");}
        }

        public object Series
        {
            get
            {
                ChartBase b = _chart.PlotArea.Charts[0];
                if (b == null)
                    return null; //\\
                if (b is BarChartBase)
                    return ((BarChartBase) b).Series;
                if (b is LineChartBase)
                    return ((LineChartBase) b).Series;
                if (b is AreaChartBase)
                    return ((AreaChartBase) b).Series;
                if (b is PieChartBase)
                    return ((PieChartBase) b).Series;
                if (b is ScatterChart)
                    return ((ScatterChart) b).Series;
                throw new NotImplementedException();
            }
        }

        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (var c = context.Read("legend"))
            {
                _legendPos =
                    c.ReadOptionalElementEnumAttribute("legendPos", "val", LegendPosition.Right,
                                                                       LegendPosition.Right);
                Entries.Clear();
                foreach (LegendEntry e in c.ReadOptionalWhile<LegendEntry>("legendEntry"))
                    Entries.Add(e);
                Layout = c.ReadOptionalElement<Layout>(Layout.xmlName);
                Overlay = c.ReadOptionalElementBooleanAttribute("overlay", false);
                ShapeProperties = c.ReadOptionalElement<ShapeProperties>("spPr");
                _txPr = c.ReadOptionalElementAs<TextBody>("txPr");
                Extension = c.ReadOptionalElement<ExtensionElement>("extLst");
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("legend"))
            {
                c.WriteOptionalElementEnumAttribute("legendPos", "val", _legendPos, LegendPosition.Right,
                                                                    LegendPosition.Right);
                foreach (LegendEntry e in Entries)
                    c.WriteElement(e);
                c.WriteOptionalElement(Layout);
                c.WriteOptionalElementBooleanAttribute("overlay", Overlay);
                c.WriteOptionalElement(ShapeProperties);
                c.WriteOptionalElementAs(_txPr, "txPr");
                c.WriteOptionalElement(Extension);
            }
        }

        #endregion

    }

}